<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Examples PDO_4D</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-4d.sql4d.html">SQL acceptable by 4D</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="refs.database.vendors.html">针对各数据库系统对应的扩展</a></div>
 <div class="up"><a href="ref.pdo-4d.html">4D (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo-4d.examples" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Examples with PDO_4D</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">Examples with PDO_4D</span> &mdash; <span class="dc-title">Examples PDO_4D</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdo-4d.examples-description">
  <p class="para">
   This basic example show how to connect, execute a query,
   read data and disconnect from a 4D SQL server.
   <div class="example" id="example-967">
    <p><strong>Example #1 Basic example with PDO_4D</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4D:host=localhost;charset=UTF-8'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pass&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Connection&nbsp;to&nbsp;the&nbsp;4D&nbsp;SQL&nbsp;server<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'CREATE&nbsp;TABLE&nbsp;test(id&nbsp;varCHAR(1)&nbsp;NOT&nbsp;NULL,&nbsp;val&nbsp;VARCHAR(10))'</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Erreur&nbsp;4D&nbsp;:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;test&nbsp;VALUES('A',&nbsp;'B')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;test&nbsp;VALUES('C',&nbsp;'D')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;test&nbsp;VALUES('E',&nbsp;'F')"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;id,&nbsp;val&nbsp;from&nbsp;test'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">());<br /><br />unset(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br />unset(</span><span style="color: #0000BB">$db</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
    Array
(
    [0] =&gt; Array
        (
            [ID] =&gt; A
            [0] =&gt; A
            [VAL] =&gt; B
            [1] =&gt; B
        )

    [1] =&gt; Array
        (
            [ID] =&gt; C
            [0] =&gt; C
            [VAL] =&gt; D
            [1] =&gt; D
        )

    [2] =&gt; Array
        (
            [ID] =&gt; E
            [0] =&gt; E
            [VAL] =&gt; F
            [1] =&gt; F
        )

)

</pre></div>
    </div>
   </div>
  </p>
 </div>

  
 <div class="refsect1 description" id="refsect1-pdo-4d.examples-description">
  <p class="para">
   This example shows how to execute a query in 4D language, 
   and how to read the result through PDO_4D.
   <div class="example" id="example-968">
    <p><strong>Example #2 Accessing 4D language from pdo_4d</strong></p>
    <div class="example-contents"><p>
     Set up a 4D method, called <em>method</em>. Make sure
     in the method properties that the option 
     <em>Available via SQL</em> is checked. The 4D code
     is the following.
    </p></div>
    <div class="example-contents">
<div class="4dcode"><pre class="4dcode">C_TEXTE($0)
$0:=Version application(*);</pre>
</div>
    </div>

    <div class="example-contents"><p>
     The PHP code to use the above 4D method is : 
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4D:host=localhost;charset=UTF-8'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pass&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Connection&nbsp;to&nbsp;the&nbsp;4D&nbsp;server<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;{FN&nbsp;method()&nbsp;AS&nbsp;VARCHAR&nbsp;}&nbsp;FROM&nbsp;_USER_SCHEMAS&nbsp;LIMIT&nbsp;1'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">());<br /><br />unset(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br />unset(</span><span style="color: #0000BB">$db</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
(
    [0] =&gt; Array
        (
            [&lt;expression&gt;] =&gt; F0011140
            [0] =&gt; F0011140
        )

)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 description" id="refsect1-pdo-4d.examples-description">
  <p class="para">
   <div class="example" id="example-969">
    <p><strong>Example #3 Escaping 4D table names</strong></p>
    <div class="example-contents"><p>
     This examples illustrates how to escape characters in a 
     4D SQL query.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4D:host=localhost;charset=UTF-8'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pass&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Connection&nbsp;to&nbsp;4D&nbsp;server&nbsp;4D<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$objects&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'['</span><span style="color: #007700">,</span><span style="color: #DD0000">']'</span><span style="color: #007700">,</span><span style="color: #DD0000">'[]'</span><span style="color: #007700">,</span><span style="color: #DD0000">']['</span><span style="color: #007700">,</span><span style="color: #DD0000">'[['</span><span style="color: #007700">,</span><span style="color: #DD0000">']]'</span><span style="color: #007700">,</span><span style="color: #DD0000">'[[['</span><span style="color: #007700">,</span><span style="color: #DD0000">']]]'</span><span style="color: #007700">,</span><span style="color: #DD0000">'TBL&nbsp;]]32[23'</span><span style="color: #007700">);<br /><br />foreach(</span><span style="color: #0000BB">$objects&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$object</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$object&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">']'</span><span style="color: #007700">,</span><span style="color: #DD0000">']]'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$object</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$object</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'CREATE&nbsp;TABLE&nbsp;IF&nbsp;NOT&nbsp;EXISTS&nbsp;['</span><span style="color: #007700">.</span><span style="color: #0000BB">$object</span><span style="color: #007700">.</span><span style="color: #DD0000">'](['</span><span style="color: #007700">.</span><span style="color: #0000BB">$object</span><span style="color: #007700">.</span><span style="color: #DD0000">']&nbsp;FLOAT)'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;[</span><span style="color: #0000BB">$object</span><span style="color: #DD0000">]&nbsp;([</span><span style="color: #0000BB">$object</span><span style="color: #DD0000">])&nbsp;VALUES&nbsp;(</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">);"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$q&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;[</span><span style="color: #0000BB">$object</span><span style="color: #DD0000">]&nbsp;FROM&nbsp;[</span><span style="color: #0000BB">$object</span><span style="color: #DD0000">]"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$q</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$q</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'DROP&nbsp;TABLE&nbsp;['</span><span style="color: #007700">.</span><span style="color: #0000BB">$object</span><span style="color: #007700">.</span><span style="color: #DD0000">'];'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
[
]]
[]]
]][
[[
]]]]
[[[
]]]]]]
TBL ]]]]32[23
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-4d.sql4d.html">SQL acceptable by 4D</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="refs.database.vendors.html">针对各数据库系统对应的扩展</a></div>
 <div class="up"><a href="ref.pdo-4d.html">4D (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
